﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;
using Kooboo.Collections;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ITaxRepository))]
    public partial class EntityRepositories:ITaxRepository
    {
       
        public IQueryable<ITax> Taxes
        {
            get
            {
                return this.DataReaderContext.Taxes;
            }
        }

        public void Post(ITax tax)
        {
            Contract.Requires(tax != null);

            var newTax = new Tax()
            {
                Name = tax.Name,
                Percent = tax.Percent,   
            };

            this.DataWriterContext.Taxes.InsertOnSubmit(newTax);

            if (tax.SubTaxes != null)
            {
                foreach (var i in tax.SubTaxes)
                {
                    var newSubTax = new SubTax();
                    newSubTax.Tax = newTax;
                    newSubTax.CountryCode = i.Country ==null?null :i.Country.CountryCode;
                    newSubTax.Percent = i.Percent;
                    newSubTax.State = i.State;

                    this.DataWriterContext.SubTaxes.InsertOnSubmit(newSubTax);
                    i.Id = newSubTax.Id;
                }
            }




            
            this.DataWriterContext.SubmitChanges();

            tax.Id = newTax.Id;
        }

        public void Put(ITax tax)
        {
            Contract.Requires(tax != null);

            var findItem = this.DataWriterContext.Taxes
                .Where(i => i.Id == tax.Id)
                .FirstOrDefault();

            if (findItem == null)
            {
                throw new KeyNotFoundException("Tax: " + tax.Id);

            }

            findItem.Name = tax.Name;
            findItem.Percent = tax.Percent;

            var result = this.DataWriterContext
                .SubTaxes
                .Where(i=>i.TaxId == findItem.Id)
                .Compare(tax.SubTaxes, (i1, i2) => i1.Id == i2.Id, (i1, i2) =>
                {
                    i1.Country = i2.Country;
                    i1.State = i2.State;
                    i1.Percent = i2.Percent;
                });

            foreach (var i in result.Added)
            {
                var newSubTax = new SubTax();
                newSubTax.Tax = findItem;
                newSubTax.CountryCode = i.Country == null ? null : i.Country.CountryCode;
                newSubTax.Percent = i.Percent;
                newSubTax.State = i.State;

                this.DataWriterContext.SubTaxes.InsertOnSubmit(newSubTax);
                this.DataWriterContext.SubmitChanges();//immediately generate subtax id
                i.Id = newSubTax.Id;
            }


            foreach (var i in result.Deleted)
            {
                this.DataWriterContext.SubTaxes.DeleteOnSubmit((SubTax)i);
            }

            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(ITax tax)
        {
            if (tax != null)
            {
                var findItem = this.DataWriterContext.Taxes
                .Where(i => i.Id == tax.Id)
                .FirstOrDefault();

                if (findItem != null)
                {
                    var subtaxes = this.DataWriterContext.SubTaxes.Where(i => i.TaxId == findItem.Id);

                    this.DataWriterContext.SubTaxes.DeleteAllOnSubmit(subtaxes);
                    

                    this.DataWriterContext.Taxes.DeleteOnSubmit(findItem);
                    this.DataWriterContext.SubmitChanges();
                }
            }
        }

    }
}
